import $ from "jquery";


export default{
    state : {
        status: "loading", // matching表示匹配，playing表示对战界面,loading表示选择游戏界面

        socket:null,//已弃用

        opponentUsername:"",
        opponentPhoto:"",
        opponentId:-10,

        game_map:null,
        a_id: 0,
        a_sx: 0,
        a_xy: 0,
        b_id: 0,
        b_sx: 0,
        b_sy: 0,
        game_way:1,
        gameObject: null,
        Loser:"none", //all,you,he
        botId:"-1",//-1代表自己打，0，1，2，代表人机
        is_me: false,//是不是我下棋
        iAmBlack: true,//我是什么颜色

        localHostPk:false,//本地下棋
        isMatching: false,//在匹配状态，非房间状态
        roomId:"-1",//房间id
        isReady:false,//房间的除了房主以外，有没有准备
        isRoomUser: false,//是房主就是黑的
        createRoomUrl:"http://10.70.128.53:7090/api/fight/room/create",//创房间地址
        removeRoomUrl:"http://10.70.128.53:7090/api/fight/room/quit/",//{roomId}
        inviteFriendUrl:"http://10.70.128.53:7090/api/fight/invite",//{邀请好友进房间}
        acceptInviteUrl:"http://10.70.128.53:7090/api/fight/accept",
        roomReadyUrl:"http://10.70.128.53:7090/api/fight/room/ready",
        startGameUrl:"http://10.70.128.53:7090/api/fight/game/start/",//{roomId}
        startMatchingUrl:"http://10.70.128.53:7090/api/matchFight/match",//开始匹配
        cancelMatchingUrl:"http://10.70.128.53:7090/api/matchFight/cancelMatch",//取消匹配
    },
    getters: {
    },
    mutations: {
    updateSocket(state,socket){//已弃用
        state.socket = socket;
      },
    updateOpponent(state,opponent){
        state.opponentUsername = opponent.username;
        state.opponentPhoto = opponent.photo;
        state.opponentId = opponent.id;

    },
    updateLocalHostPk(state,localHostPk){
        state.localHostPk = localHostPk;
    },
    updateStatus(state,status){
        state.status = status;
    },

    updateIsMe(state,is_me){
        state.is_me = is_me;
    },
    updateIAmBlack(state,iAmBlack){
        state.iAmBlack = iAmBlack;
   },
   updateIsMatching(state,isMatching){
        state.isMatching = isMatching;
   },
    updateIsReady(state,isReady){
        state.isReady = isReady;
    },
    updateGame(state,game){
        state.game_map = game.map;
        state.a_id = game.a_id;
        state.a_sx = game.a_sx;
        state.a_sy = game.a_sy;
        state.b_id = game.b_id;
        state.b_sx = game.b_sx;
        state.b_sy = game.b_sy;
        // state.game_way = game.game_way;
        state.game_way = 1;
    },
    updateGameObject(state, gameObject){
        state.gameObject = gameObject;
    },
    updateLoser(state,Loser){
        state.Loser = Loser;
    },
    updateRoomId(state,data){
        state.roomId = data.roomId;
        state.isRoomUser = data.isRoomUser;
    },
    clearAllPkInfo(state){
    state.opponentUsername = "我的对手";
    state.opponentPhoto = "https://cdn.acwing.com/media/article/image/2022/08/09/1_1db2488f17-anonymous.png";
    state.opponentId = -2;
    state.isReady = false;
    state.isRoomUser=false;
    state.roomId = "-1";
    state.gameObject = null;
    state.botId = "-1";
    state.isMatching = false;
    },
    clearOpponentPkInfo(state){
        state.opponentUsername = "我的对手";
        state.opponentPhoto = "https://cdn.acwing.com/media/article/image/2022/08/09/1_1db2488f17-anonymous.png";
        state.opponentId = -2;
        state.isReady = false;
    },
    updateBotId(state,botId){
        state.botId = botId;
    },

    },
    actions: {
        createRoom(context,data){

            $.ajax({
                type: "post",
                url: context.state.createRoomUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                success(resp) {
                    if(resp.status === "success"){//如果成功了
                        console.log("resp",resp);
                        context.commit("updateRoomId", {
                            roomId:resp.data.roomId,
                            isRoomUser:true,
                        })
                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });

        },
        removeRoom(context,data){

            $.ajax({
                type: "post",
                url: context.state.removeRoomUrl + context.state.roomId,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        context.commit("updateRoomId",{
                            roomId:"-1",
                            isRoomUser:false,
                        });
                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });

        },
        inviteFriend(context,data){

            $.ajax({
                type: "post",
                url: context.state.inviteFriendUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    roomId:context.state.roomId,
                    receiverId:data.receiverId,
                }),
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });
        },
        acceptInvite(context,data){

            $.ajax({
                type: "post",
                url: context.state.acceptInviteUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    inviteKey:data.inviteKey,
                    senderId:data.senderId,
                }),
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });

        },
        roomReady(context,data){
            $.ajax({
                type: "post",
                url: context.state.roomReadyUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    roomId:context.state.roomId,
                    status:data.status,
                }),
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });
        },
        startGame(context,data){
            $.ajax({
                type: "post",
                url: context.state.startGameUrl + context.state.roomId,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });
        },
        startMatching(context,data){
            console.log(data);
            $.ajax({
                type: "post",
                url: context.state.startMatchingUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    userId:data.userId,
                    rating:data.rating,
                }),
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });
        },
        cancelMatching(context,data){
            $.ajax({
                type: "post",
                url: context.state.cancelMatchingUrl,
                headers:{
                    Authorization: "Bearer " + data.token//授权
                },
                contentType: "application/json", // 设置请求头
                data: JSON.stringify({
                    userId:data.userId,
                    rating:data.rating,
                }),
                success(resp) {
                    if(resp.status === "success"){//如果成功了

                        data.success(resp);
                    }else {
                        data.error(resp);
                    }
                },
                error(resp){
                    data.error(resp);
                }
            });
        },

    },
    modules: {
    }
}